package com.wzgl.core.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.wzgl.core.base.Table;
import com.wzgl.core.baseModel.BaseGird;
import com.wzgl.core.utils.StringUtils;

/***
 * 格子表
 * @author Administrator
 *
 */
@Table(tableName = "tgrid", primaryKey = "fid")
public class Tgrid extends BaseGird<Tgrid> {
	private static final long serialVersionUID = -1L;
	public static final Tgrid dao = new Tgrid();
	
	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<Tgrid> paginate(int pageNumber, int pageSize,Integer fstatus,String ftitle) {
		String sql= " from tgrid where 1 = 1";
		List<Object> agrs = new ArrayList<Object>();
		if(fstatus != null){
			sql += " and fstatus = ?";
			agrs.add(fstatus);
		}
		if(!StrKit.isBlank(ftitle)){
			sql += "  and ftitle = ? ";
			agrs.add(ftitle);
		}
		sql += "  order by fdate desc";
//		if(StringUtils.isNotBlank(ftitle)){
//			return paginate(pageNumber, pageSize, "select *", "",new Object[fstatus,ftitle]);
//		}
		return paginate(pageNumber, pageSize, "select *", sql,agrs.toArray());
	}
	
	public Page<Tgrid> paginate(int pageNumber, int pageSize,Tgrid t) {
		String sql= " from tgrid where 1 = 1";
		List<Object> agrs = new ArrayList<Object>();
		if(t.getFstatus() != null){
			sql += " and fstatus = ?";
			agrs.add(t.getFstatus());
		}
		if(t.getFtype() != null) {
			sql += (" AND ftype =  ? ");
			agrs.add(t.getFtype());
		}
		if(!StrKit.isBlank(t.getFtitle())){
			sql += "  and ftitle like '%"+t.getFtitle()+"%' ";
//			agrs.add(t.getFtitle());
		}
		if(!StrKit.isBlank(t.getFmzgicId())){
			sql += "  and fmzgicId = ? ";
			agrs.add(t.getFmzgicId());
		}
		sql += "  order by fdate desc";
		return paginate(pageNumber, pageSize, "select *", sql,agrs.toArray());
	}
	
	public  Page<Tgrid> findByType(int pageNumber, int pageSize,Integer fstatus,final Integer ftype) {
		String sql= " from tgrid where 1 = 1";
		List<Object> agrs = new ArrayList<Object>();
		if(fstatus != null){
			sql += " and fstatus = ?";
			agrs.add(fstatus);
		}
		if(ftype != null){
			sql += "  and ftype = ? ";
			agrs.add(ftype);
		}
		sql += "  order by fdate desc";
//		if(StringUtils.isNotBlank(ftitle)){
//			return paginate(pageNumber, pageSize, "select *", "",new Object[fstatus,ftitle]);
//		}
		return paginate(pageNumber, pageSize, "select *", sql,agrs.toArray());
	}
	public Tgrid baseById(final String fid) {
		return dao.doFindFirst("fid = ?", fid);
	}
	
	/**
	 * 获取热点帖子
	 */
	public Page<Tgrid> getPageFire(int size) {
		return paginate(1, size, "select *", "from tgrid where fstatus = 0 and ffire = 1 order by fdate desc");
	}
	
	public Tgrid findById(final String fid) {
//		return dao.getCache(fid, new IDataLoader() {
//			@Override
//			public Object load() {
//				return dao.doFindFirst(" fid = ?", fid);
//			}
//		});
		return dao.doFindFirst(" fid = ?", fid);
	}	
}
